#!/usr/local/bin/python3
#-*-coding: utf-8-*-
#########################################################
# PassConfig feito por felipe silva                     #
#########################################################
import subprocess
import getpass
print('''
#################################################
# PassConfig WIFI By: https://freebsd.org       #
#################################################
''')
SSID = str(input("Rede>"))
PASSWORD = getpass.getpass("Senha>")
with open("/usr/home/$USER/.wifi-pass.txt", "a") as writepass:
  writepass.write(SSID)
  writepass.write("\n")
  writepass.write(PASSWORD)
  writepass.write("\n")
  ''' backup do wpa '''
with open("/etc/wpa_supplicant.conf", "r") as backup_r:
  with open("/usr/home/$USER/.wpa_backup.txt", "w") as write_w:
    write_w.write(backup_r.read())
    write_w.write("\n")
    ''' altera wpa '''
with open("/etc/wpa_supplicant.conf", "w") as modifi:
  modifi.write("network={")
  modifi.write("\n")
  modifi.write('ssid="%s"' %SSID)
  modifi.write("\n")
  modifi.write('psk="%s"' %str(PASSWORD))
  modifi.write("\n")
  modifi.write("}")
''' executa atualização do wpa e roda as config '''
log = subprocess.run("service netif restart", shell=True, stdout=subprocess.PIPE)
for x in log.stdout:
  print(x)
